import time
import pymysql


def get_time():
    time_str = time.strftime("%Y{}%m{}%d{} %X")
    return time_str.format("年","月","日")


def get_conn():
    # 建立连接
    conn = pymysql.connect(host="127.0.0.1",
                           port=3306,
                           user="root",
                           password="root",
                           database="51job",)
    # c创建游标A
    cursor = conn.cursor()
    return conn, cursor


def close_conn(conn, cursor):
    if cursor:
        cursor.close()
    if conn:
        conn.close()


def query(sql,*args):
    """

    :param sql:
    :param args:
    :return:
    """
    conn,cursor = get_conn()
    cursor.execute(sql,args)
    res = cursor.fetchall()
    close_conn(conn,cursor)
    return res


def test():
    sql = "select * from job"
    res = query(sql)
    return res[0]


def get_c2_data():
    sql = '''select region,count(region) from job
    group by region having count(region)>=1;'''
    res = query(sql)
    return res


def get_l1_data():
    sql = '''select required,salary from job
    '''
    res = query(sql)
    return res


def get_l2_data():
    sql = '''select recruiters,count(recruiters) from job
    group by recruiters having count(recruiters)>=1;
    '''
    res = query(sql)
    return res


def get_r1_data():
    sql = '''select recruiters,salary from job
    '''
    res = query(sql)
    return res


def get_r2_data():
    sql = '''select required,count(required) from job
    group by required having count(required)>=1;
    '''
    res = query(sql)
    return res


if __name__ == '__main__':
    print(get_r1_data())